home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / lib / sendpass.c < prev    next >
C/C++ Source or Header  |  1992-07-24  |  2KB  |  105 lines

  1. /*  $Revision: 1.9 $
  2. **
  3. */
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include "configdata.h"
  9. #include "nntp.h"
  10. #include "paths.h"
  11. #include "libinn.h"
  12. #include "clibrary.h"
  13. #include "macros.h"
  14.  
  15.  
  16. /*
  17. **  Send authentication information to an NNTP server.
  18. */
  19. int
  20. NNTPsendpassword(server, FromServer, ToServer)
  21.     char        *server;
  22.     FILE        *FromServer;
  23.     FILE        *ToServer;
  24. {
  25.     register FILE    *F;
  26.     register char    *p;
  27.     char        buff[SMBUF];
  28.     char        input[SMBUF];
  29.     char        *user;
  30.     char        *pass;
  31.     char        *style;
  32.     int            oerrno;
  33.  
  34.     /* What server are we interested in?  Default to the campus one. */
  35.     if (server == NULL
  36.      && (server = GetConfigValue(_CONF_SERVER)) == NULL)
  37.     return -1;
  38.  
  39.     /* Open the password file; coarse check on errno, but good enough. */
  40.     if ((F = fopen(_PATH_NNTPPASS, "r")) == NULL)
  41.     return errno == EPERM ? -1 : 0;
  42.  
  43.     /* Scan the file, skipping blank and comment lines. */
  44.     while (fgets(buff, sizeof buff, F) != NULL) {
  45.     if ((p = strchr(buff, '\n')) != NULL)
  46.         *p = '\0';
  47.     if (buff[0] == '\0' || buff[0] == COMMENT_CHAR)
  48.         continue;
  49.  
  50.     /* Parse the line. */
  51.     if ((user = strchr(buff, ':')) == NULL)
  52.         continue;
  53.     *user++ = '\0';
  54.     if ((pass = strchr(user, ':')) == NULL)
  55.         continue;
  56.     *pass++ = '\0';
  57.     if ((style = strchr(pass, ':')) != NULL) {
  58.         *style++ = '\0';
  59.         if (!EQ(style, "authinfo")) {
  60.         errno = EDOM;
  61.         break;
  62.         }
  63.     }
  64.  
  65.     if (!caseEQ(server, buff))
  66.         continue;
  67.  
  68.     if (*user) {
  69.         /* Send the first part of the command, get a reply. */
  70.         (void)fprintf(ToServer, "authinfo user %s\r\n", user);
  71.         if (fflush(ToServer) == EOF || ferror(ToServer))
  72.         break;
  73.         if (fgets(input, sizeof input, FromServer) == NULL
  74.          || atoi(input) != NNTP_AUTH_NEXT_VAL)
  75.         break;
  76.     }
  77.  
  78.     if (*pass) {
  79.         /* Send the second part of the command, get a reply. */
  80.         (void)fprintf(ToServer, "authinfo pass %s\r\n", pass);
  81.         if (fflush(ToServer) == EOF || ferror(ToServer))
  82.         break;
  83.         if (fgets(input, sizeof input, FromServer) == NULL
  84.          || atoi(input) != NNTP_AUTH_OK_VAL)
  85.         break;
  86.     }
  87.  
  88.     /* Authenticated. */
  89.     (void)fclose(F);
  90.     return 0;
  91.     }
  92.  
  93.     /* End of file without finding a password, that's okay. */
  94.     if (feof(F)) {
  95.     (void)fclose(F);
  96.     return 0;
  97.     }
  98.  
  99.     /* Save errno, close the file, fail. */
  100.     oerrno = errno;
  101.     (void)fclose(F);
  102.     errno = oerrno;
  103.     return -1;
  104. }
  105.